﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Comm;
using System.Text;
using Model.RolePopedom;
using System.Reflection;

public partial class _admin_left : AdminPageBase
{
    //模块属性集合
    private FieldInfo[] fields = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadData();
            loadMenu();
        }
    }

    /// <summary>
    /// 加载数据
    /// </summary>
    private void loadData()
    {
        lit_username.Text = myuser.Username + "&nbsp;&nbsp;您好！";
        lit_time.Text = DateTime.Now.ToString("yyyy年MM月dd日") + "&nbsp;&nbsp;星期" + getWeek(DateTime.Now.DayOfWeek.ToString());
        lit_userinfo.Text = "<a target=\"mainFrame\" href=\"admin/admin_add.aspx?id=" + myuser.ID + "\">个人设置</a>";
    }

    /// <summary>
    /// 获取星期
    /// </summary>
    /// <param name="week"></param>
    /// <returns></returns>
    private string getWeek(string week)
    {
        switch (week)
        {
            case "Monday": return "一";
            case "Tuesday": return "二";
            case "Wednesday": return "三";
            case "Thursday": return "四";
            case "Friday": return "五";
            case "Saturday": return "六";
            case "Sunday ": return "日";
            default: return "";
        }
    }

    /// <summary>
    /// 绑定菜单
    /// </summary>
    private void loadMenu()
    {
        fields = typeof(ModulePermission).GetFields();
        StringBuilder sb = new StringBuilder("<ul>");
        SiteMapNodeCollection list = Comm.CMSiteMapProvider.SiteMapProvider.RootNode.ChildNodes;
        for (int i = 0; i < list.Count; i++)
        {
            string child = getMenuChild(list[i].ChildNodes);
            if (!string.IsNullOrEmpty(child))
            {
                sb.AppendFormat("<li><a href=\"javascript:void(0);\" target=\"mainFrame\" class=\"{0}\">{1}</a>{2}</li>", list[i]["class"], list[i].Title, child);
            }
        }
        sb.Append("</ul>");

        lit_menu.Text = sb.ToString();
    }

    /// <summary>
    /// 根据key获取子菜单
    /// </summary>
    /// <returns></returns>
    private string getMenuChild(SiteMapNodeCollection nodes)
    {
        if (nodes == null || nodes.Count < 1)
        {
            return "";
        }

        StringBuilder sb = new StringBuilder("<dl class=\"ldl\">");
        ModulePermission model = new ModulePermission();
        for (int i = 0; i < nodes.Count; i++)
        {
            int id = 0;
            foreach (FieldInfo filed in fields)
            {
                if (filed.Name == nodes[i]["menuKey"])
                {
                    id = Convert.ToInt32(filed.GetValue(model));
                    break;
                }
            }

            if (!base.CheckPopedomInfo(id, (int)PermissionEnum.Select))
            {
                return "";
            }
            sb.AppendFormat("<dd><a href=\"{1}\" target=\"mainFrame\">{0}</a></dd>", nodes[i].Title, nodes[i].Url);
            if (nodes[i].ChildNodes.Count > 0)
            {
                for (int j = 0; j < nodes[i].ChildNodes.Count; j++)
                {
                    string key = nodes[i].ChildNodes[j]["menuKey"].ToString().Trim();
                    long value = Convert.ToInt64(Enum.Parse(typeof(PermissionEnum), key, true));
                    if (base.CheckPopedomInfo(id, value))
                    {
                        sb.AppendFormat("<dd><a href=\"{1}\" target=\"mainFrame\">{0}</a></dd>", nodes[i].ChildNodes[j].Title, nodes[i].ChildNodes[j].Url);
                    }
                }
            }
        }

        sb.Append("</dl>");
        
        return sb.ToString();
    }
}
